package com.ck.controller.student;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.ck.domain.Xuanze;
import com.ck.service.QuestionService;

public class ExerciseXuanzeController extends AbstractController {

	private QuestionService questionService;

	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Integer XUANZE_NUM = 5;	//题目数量.必须与QuestionServiceImpl.gainXuanzeForExercise()中的一致.
		Map model = new HashMap();
		
		Integer correct_count = 0;
		List<Xuanze> xuanzeList = new ArrayList<Xuanze>();
		
		for(int i=0; i<XUANZE_NUM; i++){
			//获取题目并记住题目的顺序
			Integer xuanzeID = Integer.parseInt(request.getParameter("xuanzeID_"+i));
			Xuanze xuanze = questionService.gainXuanze(xuanzeID);
			
			//获取用户选择的答案
			String answer_order = request.getParameter("answer_order_"+i);
			String user_answer = request.getParameter("user_answer_"+i);
			
			//保存选项的顺序
			xuanze.setAnswer_order(answer_order);
			
			//批改答案
			if( user_answer == null || "".equals(user_answer)){
				xuanze.setUser_answer(null);
				xuanze.setAnswer_result(0);		//0表示回答错误
			}else{
				xuanze.setUser_answer(user_answer);
				
				if( answer_order.charAt(Integer.parseInt(user_answer)) == 'c' ){
					xuanze.setAnswer_result(1);		//1表示回答正确
					correct_count++;
				}else{
					xuanze.setAnswer_result(0);
				}
			}
			
			//按answer_order中保存的顺序重新生成题目的选项以保证与原来的一致.
			for(int j=0; j<answer_order.length(); j++){
				String temp = null;
				//根据随机数提取出一个选项.
				switch(answer_order.charAt(j)){
				case 'c':
					temp = xuanze.getCorrect_answer();
					xuanze.setRight_answer_pos(j);		//记住正确答案的字母.
					break;
				case 'o':
					temp = xuanze.getFalse_one();
					break;
				case 'w':
					temp = xuanze.getFalse_two();
					break;
				case 'r':
					temp = xuanze.getFalse_three();
					break;
				}
				
				//将提取出的选项依次放进ABCD中.
				switch(j){
				case 0:
					xuanze.setAnswer_a(temp);
					break;
				case 1:
					xuanze.setAnswer_b(temp);
					break;
				case 2:
					xuanze.setAnswer_c(temp);
					break;
				case 3:
					xuanze.setAnswer_d(temp);
					break;
				}
			}
			
			xuanzeList.add(xuanze);
		}
		
		model.put("xuanzeList", xuanzeList);
		model.put("correct_count", correct_count);
		model.put("xuanze_num", XUANZE_NUM);
		
		//把组卷参数也传回页面
		Integer chapterID = Integer.parseInt(request.getParameter("chapterID"));
		Integer difficulty = Integer.parseInt(request.getParameter("difficulty"));
		String chapterName = questionService.gainChapter(chapterID).getName();
		
		model.put("chapterID", chapterID);
		model.put("difficulty", difficulty);
		model.put("chapterName", chapterName);
		
		return new ModelAndView("student/exerciseXuanze", model);
	}

}
